﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Helper.Enumerators;

namespace Prodma.DataAccess.Authorization
{
    public class NavigationFilter : ActionFilterAttribute
    {
        private MenuAdmins menu;
        private AuthorizationType authType;

        public NavigationFilter(MenuAdmins _menu, AuthorizationType _authType)
        {
            menu = _menu;
            authType = _authType;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            bool isAuth = false;

            if (HttpContext.Current.Session["userAuthorization"] == null)
            {
                filterContext.RequestContext.HttpContext.Response.Redirect("/account/logOn/");
                return;
            }

            RoleVm authorization = (RoleVm)HttpContext.Current.Session["userAuthorization"];

            int menuId = Convert.ToInt32(menu);

            if (authorization == null)
            {
                isAuth = false;
                filterContext.HttpContext.Response.Redirect("~/account/authorizationError/");
            }

            if (authType == AuthorizationType.AddChart && authorization.AddChart == true)
            {
                isAuth = true;
                return;
            }
            else if (authType == AuthorizationType.CompleteOrder && authorization.CompleteOrder == true)
            {
                isAuth = true;
                return;
            }
            else if (authType == AuthorizationType.AmendOrder && authorization.AmendOrder == true)
            {
                isAuth = true;
                return;
            }
            else if (authType == AuthorizationType.ConfirmOrder && authorization.ConfirmOrder == true)
            {
                isAuth = true;
                return;
            }
            else if (authType == AuthorizationType.CompleteOrder && authorization.CompleteOrder == true)
            {
                isAuth = true;
                return;
            }
            else if (authType == AuthorizationType.Search && authorization.Search == true)
            {
                isAuth = true;
                return;
            }
            else if (authType == AuthorizationType.ManageUser && authorization.ManageUser == true)
            {
                isAuth = true;
                return;
            }
            else if (authType == AuthorizationType.ClearChart && authorization.ClearChart == true)
            {
                isAuth = true;
                return;
            }
            if (isAuth == false)
            {
                filterContext.HttpContext.Response.Redirect("~/account/authorizationError/");
            }
        }
    }
}
